import 'package:flutter/material.dart';
import 'package:flutter_frontend/main.dart';
import 'package:flutter_frontend/widgets/my_dialog.dart';
import 'package:permission_handler/permission_handler.dart';

enum PermissionType {
  camera,
}

class PermissionUtils {
  PermissionUtils._();

  static Future<bool> getPermissionStatus(PermissionType permissionType) async {
    late Permission permission;
    late String title;
    if (permissionType == PermissionType.camera) {
      title = '相机';
      permission = Permission.camera;
    }
    PermissionStatus status = await permission.status;
    if (status.isGranted) {
      return true;
    } else if (status.isDenied) {
      status = await permission.request();
      if (status.isGranted) {
        return true;
      } else if (status.isDenied) {
        return false;
      } else if (status.isPermanentlyDenied) {
        final BuildContext? context = navigatorKey.currentContext;
        if (context == null) {
          openAppSettings();
        } else {
          MyDialog.showWaringDialog(context, content: 'App无法获取$title权限，请在设置页面设置允许访问', confirmText: '打开设置').then((value) {
            if (value == true) {
              openAppSettings();
            }
          });
        }
      }
    }
    return false;
  }
}
